layui.define(['form','table','element'], function(exports){
  var $ = layui.$,form = layui.form,table = layui.table, element = layui.element;

  var userId = $('input[name="userId"]').val();
  var relationUserId = $('input[name="relationUserId"]').val();

  table.render({
      elem: '#LAY-table'
      ,url: layui.setter.webRoot + '/agent/relation/user/busi_config/list.data'
      ,where:{userId:userId,relationUserId:relationUserId}
      ,method: 'post'
      ,parseData: function (res) {
          return {
              "code": res.code,
              "msg": res.msg,
              "count": res.body.total,
              "data": res.body.data
          };
      }
      ,response: {statusCode: 200}
      ,cols: [[
          {field: 'businessType', title: '业务类型'}
          ,{field: 'businessTypeName', title: '业务类型名称'}
          ,{field: 'incomeRatio', title: '分成比例',templet: function (record){
                  return record.incomeRatio + '%';
              }}
		  ,{field: 'editFeeState', title: '编辑价格模版',templet: function (record){
            if(record.editFeeState == 2){
				return "是";
			}else{
				return "否";
			}
        }}
          ,{field: 'createDate', title: '创建时间'}
		,{title: '操作', width: 120, align:'center', fixed: 'right',templet:function (record){
			var html = '<a class="layui-btn layui-btn-xs" lay-event="updateBusiConfig">修改</a>';
              html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delBusiConfig">删除</a>';
              return html;
          }}
      ]]
      ,page: true
      ,limit: 30
      ,height: 'full-220'
      ,text: '对不起，加载出现异常！'
  });
  
  table.render({
    elem: '#LAY-table-func-model'
    ,url: layui.setter.webRoot + '/agent/relation/user/func_model/list.data'
    ,where:{userId:userId,relationUserId:relationUserId}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
          {field: 'funcModelId', title: 'ID'}
          ,{field: 'name', title: '功能模块名称',templet:function (record){
                  return record.funcModel.name;
              }}
          ,{field: 'type', title: '功能类型',templet:function (record){
                  return record.funcModel.typeName;
              }}
          ,{field: 'iconUrl', title: 'ICON图标',templet:function (record){
              if(record.funcModel.iconUrl) {
                  return '<img src="' + record.funcModel.iconUrl + '" style="max-height:100%;margin:auto;display:block;">';
              }else{
                  return '<img src="'+layui.setter.webRoot + '/layuiadmin/style/res/default01.png" style="max-height:100%;margin:auto;display:block;"/>';
              }
          }}
          ,{field: 'stateName', title: '状态'}
          ,{field: 'createDate', title: '创建时间'}
		  ,{title: '操作', width: 120, align:'center', fixed: 'right',templet:function (record){
  			var html ='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delFuncModel">删除</a>';
                return html;
            }}
    ]]
    ,page: false
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  table.render({
    elem: '#LAY-table-device'
    ,url: layui.setter.webRoot + '/agent/relation/user/device/list.data'
    ,where:{userId:userId,relationUserId:relationUserId}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
        {field: 'deviceSn', title: '设备序列号'}
        ,{field: 'deviceTypeName', title: '设备类型',templet: function (record){
            return record.device.deviceTypeName;
        }}
        ,{field: 'deviceFunNames', title: '设备功能项',templet: function (record){
                return record.device.deviceFunNames;
            }}
    	,{field: 'networkType', title: '联网类型'	,templet: function (record){
            return record.device.networkTypeName;
        }}
		,{field: 'placeName', title: '部署网点',templet: function (record){
	        return record.device.placeName;
	    }}
    	,{field: 'deployDate', title: '部署时间',templet: function (record){
	        return record.device.deployDate;
	    }}
        ,{field: 'createDate', title: '创建时间'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  table.render({
      elem: '#LAY-table-place'
      ,url: layui.setter.webRoot + '/agent/relation/user/place/list.data'
      ,where:{userId:userId,relationUserId:relationUserId}
      ,method: 'post'
      ,parseData: function (res) {
          return {
              "code": res.code,
              "msg": res.msg,
              "count": res.body.total,
              "data": res.body.data
          };
      }
      ,response: {statusCode: 200}
      ,cols: [[
          {field: 'id', title: '网点ID'}
          ,{field: 'name', title: '网点名称'}
          ,{field: 'roleTypeName', title: '角色'}
          ,{field: 'incomeRatioNote', title: '分成信息'}
          ,{field: 'typeNames', title: '网点类型'}
          ,{field: 'areaName', title: '所在区域'}
          ,{field: 'createDate', title: '创建时间'}
      ]]
      ,page: true
      ,limit: 30
      ,height: 'full-220'
      ,text: '对不起，加载出现异常！'
  });
  
  element.on('tab(tab-info)', function (data) {
      if(data.index== 0){
		table.reload('LAY-table')
      }else if(data.index== 1){
		table.reload('LAY-table-func-model')
  	  }else if(data.index== 2){
		table.reload('LAY-table-device')
	  }else if(data.index== 3){
		table.reload('LAY-table-place')
	  }
  });
  
  //事件
  var active = {
	 updateRoleType:function(){
		 layer.open({
	          type: 2
	          ,title: '修改角色'
	          ,content: layui.setter.webRoot + '/agent/relation/user/role_type/update.html?userId='+userId+'&relationUserId='+relationUserId
	          ,maxmin: true
	          ,area: ['500px', '450px']
	          ,btn: ['确定', '取消']
	          ,yes: function(index, layero){
	              var iframeWindow = window['layui-layer-iframe'+ index]
	                  ,submitID = 'LAY-form-submit'
	                  ,submit = layero.find('iframe').contents().find('#'+ submitID);
	              iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
	                  var field = data.field;
	                  $.ajax({
	                      type: "post",
	                      url: layui.setter.webRoot + '/agent/relation/user/role_type/update.do',
	                      data:field,
	                      dataType:"json",
	                      success:function(res){
	                          if(res.code == 200){
	                              layer.msg('新增成功',{icon: 1});
	                              layer.close(index); //关闭弹层
								  location.reload();
	                          }else{
	                              layer.msg(res.msg,{icon: 2});
	                          }
	                      }
	                  });
	              });
	              submit.trigger('click');
	          }
	      });
	 }
     ,addFuncModel:function (){
          layer.open({
              type: 2
              ,title: '新增功能模块'
              ,content: layui.setter.webRoot + '/agent/relation/user/func_model/add.html?userId='+userId+'&relationUserId='+relationUserId
              ,maxmin: true
              ,area: ['500px', '450px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/agent/relation/user/func_model/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.msg('新增成功',{icon: 1});
                                  table.reload('LAY-table-func-model')
                                  layer.close(index); //关闭弹层
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,delFuncModel:function(obj){
          layer.confirm('确定要移除该功能模块吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/agent/relation/user/func_model/del.do',
                  data:{userId:userId,relationUserId:relationUserId,funcModelId:obj.data.funcModelId},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          obj.del();
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  table.on('tool(LAY-table-func-model)', function(obj){
		var type = obj.event ;
	 	active[type] ? active[type].call(this,obj) : '';
	});
  
  exports('agent_relation_user', {})
});